package hiatao.puzzle.activity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import haitao.puzzle.R;
import hiatao.puzzle.adapter.StatPagerAdapter;
import hiatao.puzzle.fragment.HomeFragment;
import hiatao.puzzle.fragment.MeFragment;
import hiatao.puzzle.fragment.RankFragment;
import hiatao.puzzle.utils.StatusBarCompat;
import hiatao.puzzle.utils.Tools;



/**   
* @Description:  选择球场
* @author admin 
* @date 2014-11-4 下午2:22:18 
* @version V1.0   
*/
public class MainActivity extends FragmentActivity {

	private HomeFragment homeFragment;
	private RankFragment talentFragment;
	private MeFragment meFragment;

	private int index=0;

	private ViewPager pager;
	private Fragment  fragments[];

	private int norId[]= {R.drawable.ic_tab_home_normal,R.drawable.ic_tab_school_normal,R.drawable.ic_tab_me_normal};
	private int preId[]= {R.drawable.ic_tab_home_selected,R.drawable.ic_tab_school_selected,R.drawable.ic_tab_me_selected};
	private LinearLayout Ly[];
	private LinearLayout home_lay;
	private LinearLayout talent_lay;
	private LinearLayout me_lay;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViews();
		init();
		StatusBarCompat.compat(this, getResources().getColor(R.color.primary));

	}


	protected void findViews() {
		pager = (ViewPager) findViewById(R.id.viewpager);
		home_lay = (LinearLayout) findViewById(R.id.tab_home);
		talent_lay = (LinearLayout) findViewById(R.id.tab_talent);
		me_lay = (LinearLayout) findViewById(R.id.tab_me);
		Ly = new LinearLayout[]{home_lay,talent_lay,me_lay};
	}

	protected void init() {
		
		homeFragment = new HomeFragment();
		talentFragment= new RankFragment();
		meFragment= new MeFragment();
		fragments =new Fragment[] {homeFragment,talentFragment,meFragment};
		pager.setAdapter(new StatPagerAdapter(getSupportFragmentManager(),fragments));
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				index = position;
				setTabSelect(position);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	public void onTabClicked(View view){
    	int tempinex=0;
    	switch (view.getId()) {
		case R.id.tab_home:
			tempinex=0;
			break;
		case R.id.tab_talent:
			tempinex =1;
			break;
		case R.id.tab_me:
			tempinex =2;
			break;
		default:
			break;
		}
    	if(tempinex!=index){
    		index=tempinex;
    		pager.setCurrentItem(index, false);
    	}
    }
    
   private void setTabSelect(int position){
    	for(int i =0;i<3;i++){
    		if(position==i){
        		((ImageView)Ly[i].getChildAt(0)).setImageResource(preId[i]);
        		((TextView)Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.head_color));
    		}else{
        		((ImageView)Ly[i].getChildAt(0)).setImageResource(norId[i]);
        		((TextView)Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.menu_text_normal));
    		}
    	}
    }
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == Tools.PICLIB && data!=null) {
			Uri uri = data.getData();  
			//Tools.getToPictureCut(this,uri);
			String[] proj = { MediaStore.Images.Media.DATA };
			// 好像是android多媒体数据库的封装接口，具体的看Android文档
			Cursor cursor = managedQuery(uri, proj, null, null, null);
			int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			// 将光标移至开头 ，这个很重要，不小心很容易引起越界
			cursor.moveToFirst();
			// 最后根据索引值获取图片路径
			String path = cursor.getString(column_index);
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, ImageCutActivity.class);
			intent.putExtra("URL", "file:///" + path);
			startActivity(intent);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
